<?php

namespace app\dao;

abstract class BaseDao
{
    /**
     * 当前表名别名
     * @var string
     */
    protected $alias;

    /**
     * join表别名
     * @var string
     */
    protected $joinAlis;

    abstract protected function setModel(): string;

    /**
     * 获取模型
     * @return BaseModel
     */
    protected function getModel()
    {
        return app()->make($this->setModel());
    }

    /**
     * 获取主键
     * @return mixed
     */
    protected function getPk()
    {
        return $this->getModel()->getPk();
    }

    /**
     * 插入数据
     * @param array $data
     * @return mixed
     */
    public function save(array $data)
    {
        $info = $this->getModel()::create($data);
        return $this->getModel()->find($info['id']);
    }

    /**
     * 修改数据
     * @param array $data
     * @param $condition
     * @param string $conditionKey
     * @return mixed
     */
    public function update(array $data, $condition,$conditionKey = 'id'){
        $this->getModel()::update($data, [$conditionKey => $condition]);
    }


    public function pagination(object $query ,int $page,int $perPage)
    {
        return [
            'pagination' => ['page' => $page, 'perPage' => $perPage, 'total' => $query->count()],
            'list'       => $query->page($page, $perPage)->select()
        ];
    }
}